<template>
  <!-- 4 lx属性管理 -->
  <div class="breadcrumb">
    <Breadcrumb :aaa="aaa" :msgg="msg" :txt="txt" :path1="path1" />
  </div>

  <!-- 主内容 -->
  <!-- 1 -->
  <el-card style="margin: 20px">
    <div style="display: flex; justify-content: space-between">
      <div style="margin-top: 10px">数据列表</div>
      <div><el-button @click="onAddUser" type="primary">添加</el-button></div>
    </div>
  </el-card>
  <!-- 2 -->
  <div class="dataList" style="margin: 20px">
    <!-- 数据的展示 -->
    <el-table :data="tableData" style="width: 100%" :border="true">
      <el-table-column prop="id" label="编号" width="100" />
      <el-table-column prop="name" label="类型名称" />
      <el-table-column prop="attributeCount" label="属性数量" width="180" />
      <el-table-column prop="paramCount" label="参数数量" width="180" />
      <el-table-column label="设置" width="300" flex="right">
        <template #default="scope">
          <el-button @click="gotoProductArrList(scope.row)">属性列表</el-button>
          <el-button @click="gotoProductArrbute(scope.row)">参数列表</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" flex="right">
        <template #default="scope">
          <el-button @click="onEdit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="delData(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination v-model:current-page="pagination.pageNum" class="pagination" v-model:page-size="pagination.pageSize"
      background :page-sizes="[1, 5, 10, 20]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" style="margin-top: 20px" />

    <!-- 编辑组件 -->
    <Edit :visible="visible" :form="modalForm" @cancel="onCloseDialog" @confirm="onConfirmDialog"></Edit>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import type { Ref } from "vue";
import * as api from "../../request/api";
import useTable from "../../hooks/useTables";
import Edit from "../../components/productAttr/model.vue";
// import Edit from "../../components/ProductAttr/edit.vue";
import type { IProductAtrrListItem } from "../../request/PmsProductAttributeCategoryController/index";
import { ElMessage, ElMessageBox } from "element-plus";
import Breadcrumb from "../../components/breadcrumb.vue";
import router from "../../router";
let msg = ref("属性管理");
let txt = ref("商品");
let path1 = ref("pms/addProduct");
let aaa = ref(0);

// 新增
let add = false; // false编辑 true 新增
// 模态框
let modalForm = ref({}) as Ref<IProductAtrrListItem>;

let {
  tableData,
  getTableData,
  pagination,
  handleSizeChange,
  handleCurrentChange,
} = useTable(api.productAttr.getProductCategoryList);

// 弹框
let visible = ref(false);

// 添加用户
let onAddUser = () => {
  let newUser = {
    name: "",
  } as IProductAtrrListItem;
  modalForm.value = newUser;
  add = true;
  visible.value = true;
};
// 删除
let delData = (id: number) => {
  ElMessageBox.confirm("是否确认提交!", "提示", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(async (add) => {
      if (add) {
        await api.productAttr.deleteProductAtrr(id); //删除
      }
      ElMessage({
        type: "success",
        message: "删除成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};
// 编辑
let onEdit = (row: IProductAtrrListItem) => {
  // console.log(row);
  add = false;
  modalForm.value = row;
  // console.log(modalForm);
  visible.value = true;
};
// 点击去往属性列表

// 点击去往分配菜单
let gotoProductArrList = (id: any) => {
  console.log(id);
  router.push({
    path: "/pms/productAttrList",
    query: {
      cid: id.id,
      name: id.name,
      type: 1,
    },
  });
};
// 点击去往参数列表
let gotoProductArrbute = (id: any) => {
  console.log(id);
  router.push({
    path: "/pms/productAttrList",
    query: {
      cid: id.id,
      name: id.name,
      type: 1,
    },
  });
};
// 子级emit组件传过来的事件
let onCloseDialog = () => {
  visible.value = false;
};

// 确认了输入框的内容之后
// 需要发送请求
let onConfirmDialog = (row: IProductAtrrListItem) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否确认提交!", "提示", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(async () => {
      // 发请求
      // 去判断 是 编辑 还是添加
      if (add) {
        await api.productAttr.addProductAtrr(row); // 新增
      } else {
        await api.productAttr.updataProductAtrr(row); // 编辑
      }
      visible.value = false;
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};

getTableData();
</script>

<style scoped lang="scss">
.role {
  padding: 20px 15px 20px 15px;
  background-color: #fff;
  margin-bottom: 20px;

  .role_top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}

.btn {
  padding: 20px 15px 20px 15px;
  background-color: #fff;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
}

/* :deep .el-input {
  width: 200px !important;
} */

:deep .el-button {
  font-size: 12px !important;
}

.breadcrumb {
  height: 45px;
  border-bottom: 1px solid #ebeef5;
  font-size: 20px;
}


//table表格
:deep .el-table .el-table__cell {
  text-align: center !important;
}

:deep .el-table thead {
  font-weight: bold !important;
}

//分页
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
